﻿using System;
using System.Collections.Generic;
using System.Text;

using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Index;

using ActiveDocument.Persistence;
using ActiveDocument.Queries;

namespace ActiveDocument.SearchEngine
{
    public class SearchEngine
    {
        public List<T> Search<T>(IQuery Query, Order[] Order, int? start, int? stop) where T : class, new()
        {
            Term term = new Term("type", typeof(T).Name);
            TermQuery tq = new TermQuery(term);
            Hits hits = SearchEngineIndex.Searcher.Search(tq);

            int begin = start.HasValue ? start.Value : 1;
            int end = stop.HasValue ? stop.Value : hits.Length();

            List<T> all = new List<T>(end - begin);
            for (int i = begin - 1, j = 0; i < end - 1; i++, j++)
                all.Add(ActiveDocumentRestorer.ConvertFromDocument<T>(hits.Doc(i)));
            return all;

        }

        public void Save(object obj)
        {
            SearchEngineIndex.Add(ActiveDocumentConverter.ConvertToDocument(obj));
        }

        public void Update(object obj)
        {

        }

        public void Delete(object obj)
        {

        }
    }
}
